Repetier-Firmware  1.0.0
gcode.h
Go to the documentation of this file.
1 /*
2  This file is part of Repetier-Firmware.
3 
4  Repetier-Firmware is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  Repetier-Firmware is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with Repetier-Firmware. If not, see <http://www.gnu.org/licenses/>.
16 
17 */
18 #ifndef _GCODE_H
19 #define _GCODE_H
20 
21 #define MAX_CMD_SIZE 96
22 #define ARRAY_SIZE(_x) (sizeof(_x)/sizeof(_x[0]))
23 
25 
26 class SDCard;
27 class Commands;
28 
29 
30 
31 
32 
34  Stream *stream;
35 public:
36  SerialGCodeSource(Stream *p);
37  virtual bool isOpen();
38  virtual bool supportsWrite();
39  virtual bool closeOnError(); // return true if the channel can not interactively correct errors.
40  virtual bool dataAvailable(); // would read return a new byte?
41  virtual int readByte();
42  virtual void writeByte(uint8_t byte);
43  virtual void close();
44 };
45 //#pragma message "Sd support: " XSTR(SDSUPPORT)
46 #if SDSUPPORT
47 class SDCardGCodeSource: public GCodeSource {
48  public:
49  virtual bool isOpen();
50  virtual bool supportsWrite();
51  virtual bool closeOnError(); // return true if the channel can not interactively correct errors.
52  virtual bool dataAvailable(); // would read return a new byte?
53  virtual int readByte();
54  virtual void writeByte(uint8_t byte);
55  virtual void close();
56 };
57 #endif
58 
60  public:
61  FSTRINGPARAM(pointer);
62  volatile bool finished;
64 
66  virtual bool isOpen();
67  virtual bool supportsWrite();
68  virtual bool closeOnError(); // return true if the channel can not interactively correct errors.
69  virtual bool dataAvailable(); // would read return a new byte?
70  virtual int readByte();
71  virtual void writeByte(uint8_t byte);
72  virtual void close();
73 
77  void executeCommands(FSTRINGPARAM(data),bool waitFinish,int action);
78 };
79 
80 #if NEW_COMMUNICATION
82 extern SerialGCodeSource serial0Source;
83 #if BLUETOOTH_SERIAL > 0
84 extern SerialGCodeSource serial1Source;
85 #endif
86 #if SDSUPPORT
87 extern SDCardGCodeSource sdSource;
88 #endif
89 #endif
90 
91 class GCode // 52 uint8_ts per command needed
92 {
93  uint16_t params;
94  uint16_t params2;
95 public:
96  uint16_t N;
97  uint16_t M;
98  uint16_t G;
99  float X;
100  float Y;
101  float Z;
102  float E;
103  float F;
104  int32_t S;
105  int32_t P;
106  float I;
107  float J;
108  float R;
109  float D;
110  float C;
111  float H;
112  float A;
113  float B;
114  float K;
115  float L;
116  float O;
117 
118  char *text;
119  //moved the byte to the end and aligned ints on short boundary
120  // Old habit from PC, which require alignments for data types such as int and long to be on 2 or 4 byte boundary
121  // Otherwise, the compiler adds padding, wasted space.
122  uint8_t T; // This may not matter on any of these controllers, but it can't hurt
123  // True if origin did not come from serial console. That way we can send status messages to
124  // a host only if he would normally not know about the mode switch.
126  inline bool hasM()
127  {
128  return ((params & 2)!=0);
129  }
130  inline bool hasN()
131  {
132  return ((params & 1)!=0);
133  }
134  inline bool hasG()
135  {
136  return ((params & 4)!=0);
137  }
138  inline bool hasX()
139  {
140  return ((params & 8)!=0);
141  }
142  inline void unsetX() {
143  params &= ~8;
144  }
145  inline bool hasY()
146  {
147  return ((params & 16)!=0);
148  }
149  inline void unsetY() {
150  params &= ~16;
151  }
152  inline bool hasZ()
153  {
154  return ((params & 32)!=0);
155  }
156  inline void unsetZ() {
157  params &= ~32;
158  }
159  inline bool hasNoXYZ()
160  {
161  return ((params & 56)==0);
162  }
163  inline bool hasE()
164  {
165  return ((params & 64)!=0);
166  }
167  inline bool hasF()
168  {
169  return ((params & 256)!=0);
170  }
171  inline bool hasT()
172  {
173  return ((params & 512)!=0);
174  }
175  inline bool hasS()
176  {
177  return ((params & 1024)!=0);
178  }
179  inline bool hasP()
180  {
181  return ((params & 2048)!=0);
182  }
183  inline bool isV2()
184  {
185  return ((params & 4096)!=0);
186  }
187  inline bool hasString()
188  {
189  return ((params & 32768)!=0);
190  }
191  inline bool hasI()
192  {
193  return ((params2 & 1)!=0);
194  }
195  inline bool hasJ()
196  {
197  return ((params2 & 2)!=0);
198  }
199  inline bool hasR()
200  {
201  return ((params2 & 4)!=0);
202  }
203  inline bool hasD()
204  {
205  return ((params2 & 8)!=0);
206  }
207  inline bool hasC()
208  {
209  return ((params2 & 16)!=0);
210  }
211  inline bool hasH()
212  {
213  return ((params2 & 32)!=0);
214  }
215  inline bool hasA()
216  {
217  return ((params2 & 64)!=0);
218  }
219  inline bool hasB()
220  {
221  return ((params2 & 128)!=0);
222  }
223  inline bool hasK()
224  {
225  return ((params2 & 256)!=0);
226  }
227  inline bool hasL()
228  {
229  return ((params2 & 512)!=0);
230  }
231  inline bool hasO()
232  {
233  return ((params2 & 1024)!=0);
234  }
235  inline long getS(long def)
236  {
237  return (hasS() ? S : def);
238  }
239  inline long getP(long def)
240  {
241  return (hasP() ? P : def);
242  }
243  inline void setFormatError() {
244  params2 |= 32768;
245  }
246  inline bool hasFormatError() {
247  return ((params2 & 32768)!=0);
248  }
249  void printCommand();
250  bool parseBinary(uint8_t *buffer,bool fromSerial);
251  bool parseAscii(char *line,bool fromSerial);
252  void popCurrentCommand();
253  void echoCommand();
255  static GCode *peekCurrentCommand();
257  static void readFromSerial();
258  static void pushCommand();
259  static void executeFString(FSTRINGPARAM(cmd));
260  static uint8_t computeBinarySize(char *ptr);
261  static void fatalError(FSTRINGPARAM(message));
262  static void reportFatalError();
263  static void resetFatalError();
264  inline static bool hasFatalError() {
265  return fatalErrorMsg != NULL;
266  }
267  static void keepAlive(enum FirmwareState state);
268  static uint32_t keepAliveInterval;
269  friend class SDCard;
270  friend class UIDisplay;
271  static FSTRINGPARAM(fatalErrorMsg);
272  friend class GCodeSource;
273 protected:
274  void debugCommandBuffer();
275  void checkAndPushCommand();
276  static void requestResend();
277  inline float parseFloatValue(char *s)
278  {
279  char *endPtr;
280  while(*s == 32) s++; // skip spaces
281  float f = (strtod(s, &endPtr));
282  if(s == endPtr) f=0.0; // treat empty string "x " as "x0"
283  return f;
284  }
285  inline long parseLongValue(char *s)
286  {
287  char *endPtr;
288  while(*s == 32) s++; // skip spaces
289  long l = (strtol(s, &endPtr, 10));
290  if(s == endPtr) l=0; // treat empty string argument "p " as "p0"
291  return l;
292  }
293 
294  static GCode commandsBuffered[GCODE_BUFFER_SIZE];
295  static uint8_t bufferReadIndex;
296  static uint8_t bufferWriteIndex;
297  static uint8_t commandReceiving[MAX_CMD_SIZE];
299  static uint8_t sendAsBinary;
300  static uint8_t commentDetected;
301  static uint8_t binaryCommandSize;
303  static uint32_t actLineNumber;
304  static volatile uint8_t bufferLength;
305  static uint8_t formatErrors;
307 #if NEW_COMMUNICATION == 0
308  static int8_t waitingForResend;
309  static uint32_t lastLineNumber;
312 #else
313 public:
314  GCodeSource *source;
315 #endif
316 };
317 
318 
319 #if JSON_OUTPUT
320 #include "SdFat.h"
321 // Struct to hold Gcode file information 32 bytes
322 #define GENBY_SIZE 16
323 class GCodeFileInfo {
324 public:
325  void init(SdBaseFile &file);
326 
327  unsigned long fileSize;
328  float objectHeight;
329  float layerHeight;
330  float filamentNeeded;
331  char generatedBy[GENBY_SIZE];
332 
333  bool findGeneratedBy(char *buf, char *genBy);
334  bool findLayerHeight(char *buf, float &layerHeight);
335  bool findFilamentNeed(char *buf, float &filament);
336  bool findTotalHeight(char *buf, float &objectHeight);
337 };
338 #endif
339 
340 #endif
341 
virtual void writeByte(uint8_t byte)
Definition: gcode.cpp:1447
bool hasO()
Definition: gcode.h:231
bool hasD()
Definition: gcode.h:203
SDCardGCodeSource sdSource
Definition: SDCard.cpp:29
virtual bool supportsWrite()
true if write is a non dummy function
Definition: gcode.cpp:1435
bool hasZ()
Definition: gcode.h:152
uint16_t G
G-code G value if set.
Definition: gcode.h:98
Definition: ui.h:588
bool hasY()
Definition: gcode.h:145
volatile bool finished
Definition: gcode.h:62
Definition: gcode.h:33
Definition: gcode.h:91
void unsetX()
Definition: gcode.h:142
float L
G-code L value if set.
Definition: gcode.h:115
static uint8_t bufferWriteIndex
Write position in gcode_buffer.
Definition: gcode.h:296
static uint8_t bufferReadIndex
Read position in gcode_buffer.
Definition: gcode.h:295
static uint8_t sendAsBinary
Flags the command as binary input.
Definition: gcode.h:299
bool hasM()
Definition: gcode.h:126
float R
G-code R value if set.
Definition: gcode.h:108
bool hasN()
Definition: gcode.h:130
float H
G-code H value if set.
Definition: gcode.h:111
bool hasNoXYZ()
Definition: gcode.h:159
void unsetY()
Definition: gcode.h:149
SerialGCodeSource(Stream *p)
Definition: gcode.cpp:1429
bool hasL()
Definition: gcode.h:227
bool hasB()
Definition: gcode.h:219
bool hasFormatError()
Definition: gcode.h:246
char * text
Text message of g-code if present.
Definition: gcode.h:118
float Y
G-code Y value if set.
Definition: gcode.h:100
float Z
G-code Z value if set.
Definition: gcode.h:101
bool isV2()
Definition: gcode.h:183
long getP(long def)
Definition: gcode.h:239
#define MAX_CMD_SIZE
Definition: gcode.h:21
bool hasC()
Definition: gcode.h:207
float E
G-code E value if set.
Definition: gcode.h:102
static millis_t timeOfLastDataPacket
Time, when we got the last data packet. Used to detect missing uint8_ts.
Definition: gcode.h:310
Definition: gcode.h:24
FlashGCodeSource flashSource
Definition: gcode.cpp:1320
long parseLongValue(char *s)
Definition: gcode.h:285
Stream * stream
Definition: gcode.h:34
Definition: Communication.h:40
float X
G-code X value if set.
Definition: gcode.h:99
virtual bool isOpen()
Definition: gcode.cpp:1432
bool hasT()
Definition: gcode.h:171
virtual int readByte()
Definition: gcode.cpp:1444
int32_t P
G-code P value if set.
Definition: gcode.h:105
static uint32_t lastLineNumber
Last line number received.
Definition: gcode.h:309
bool internalCommand
Definition: gcode.h:125
virtual bool closeOnError()
Definition: gcode.cpp:1438
static millis_t lastBusySignal
When was the last busy signal.
Definition: gcode.h:306
uint16_t M
G-code M value if set.
Definition: gcode.h:97
FirmwareState
Definition: gcode.h:24
bool hasF()
Definition: gcode.h:167
static bool hasFatalError()
Definition: gcode.h:264
Definition: gcode.h:24
float C
G-code C value if set.
Definition: gcode.h:110
bool hasS()
Definition: gcode.h:175
Definition: Commands.h:27
uint16_t N
Line number reduced to 16 bit.
Definition: gcode.h:96
virtual void close()
Definition: gcode.cpp:1450
void setFormatError()
Definition: gcode.h:243
Definition: gcode.h:24
uint32_t millis_t
Definition: HAL.h:158
Definition: gcode.h:24
bool hasA()
Definition: gcode.h:215
float K
G-code K value if set.
Definition: gcode.h:114
uint8_t T
Definition: gcode.h:122
Definition: gcode.h:59
void unsetZ()
Definition: gcode.h:156
static int8_t waitingForResend
Waiting for line to be resend. -1 = no wait.
Definition: gcode.h:308
static uint8_t commandsReceivingWritePosition
Writing position in gcode_transbuffer.
Definition: gcode.h:298
float F
G-code F value if set.
Definition: gcode.h:103
static uint32_t keepAliveInterval
Definition: gcode.h:268
bool hasH()
Definition: gcode.h:211
float I
G-code I value if set.
Definition: gcode.h:106
int actionOnFinish
Definition: gcode.h:63
static volatile uint8_t bufferLength
Number of commands stored in gcode_buffer.
Definition: gcode.h:304
bool hasP()
Definition: gcode.h:179
int32_t S
G-code S value if set.
Definition: gcode.h:104
bool hasE()
Definition: gcode.h:163
static uint8_t formatErrors
Number of sequential format errors.
Definition: gcode.h:305
Definition: gcode.h:24
static uint32_t actLineNumber
Line number of current command.
Definition: gcode.h:303
bool hasR()
Definition: gcode.h:199
float J
G-code J value if set.
Definition: gcode.h:107
uint16_t params2
Definition: gcode.h:94
static uint8_t binaryCommandSize
Expected size of the incoming binary command.
Definition: gcode.h:301
float B
G-code B value if set.
Definition: gcode.h:113
virtual bool dataAvailable()
Definition: gcode.cpp:1441
#define FSTRINGPARAM(var)
Definition: HAL.h:53
bool hasK()
Definition: gcode.h:223
bool hasString()
Definition: gcode.h:187
float parseFloatValue(char *s)
Definition: gcode.h:277
float D
G-code D value if set.
Definition: gcode.h:109
bool hasJ()
Definition: gcode.h:195
uint16_t params
Definition: gcode.h:93
#define GCODE_BUFFER_SIZE
Definition: Repetier.h:324
float A
G-code A value if set.
Definition: gcode.h:112
bool hasI()
Definition: gcode.h:191
bool hasX()
Definition: gcode.h:138
bool hasG()
Definition: gcode.h:134
static uint8_t wasLastCommandReceivedAsBinary
Was the last successful command in binary mode?
Definition: gcode.h:311
static bool waitUntilAllCommandsAreParsed
Don&#39;t read until all commands are parsed. Needed if gcode_buffer is misused as storage for strings...
Definition: gcode.h:302
long getS(long def)
Definition: gcode.h:235
static uint8_t commentDetected
Flags true if we are reading the comment part of a command.
Definition: gcode.h:300
float O
G-code O value if set.
Definition: gcode.h:116